home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / ole2book.zip / CHAP11.ZIP / CHAP11 / HSCHMOO / IPERSTOR.CPP < prev    next >
C/C++ Source or Header  |  1993-06-15  |  6KB  |  253 lines

  1. /*
  2.  * IPERSTOR.CPP
  3.  * Schmoo Figure Handler Chapter 11
  4.  *
  5.  * Implementation of the IPersistStorage interface that we expose on the
  6.  * Figure object.
  7.  *
  8.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Software Design Engineer
  11.  * Microsoft Systems Developer Relations
  12.  *
  13.  * Internet  :  kraigb@microsoft.com
  14.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  15.  */
  16.  
  17.  
  18. #include "hschmoo.h"
  19.  
  20.  
  21. /*
  22.  * CImpIPersistStorage:CImpIPersistStorage
  23.  * CImpIPersistStorage::~CImpIPersistStorage
  24.  *
  25.  * Constructor Parameters:
  26.  *  pObj            LPVOID pointing to the object we live in.
  27.  *  punkOuter       LPUNKNOWN of the controlling unknown.
  28.  */
  29.  
  30. CImpIPersistStorage::CImpIPersistStorage(LPCFigure pObj, LPUNKNOWN punkOuter)
  31.     {
  32.     m_cRef=0;
  33.     m_pObj=pObj;
  34.     m_punkOuter=punkOuter;
  35.     return;
  36.     }
  37.  
  38.  
  39. CImpIPersistStorage::~CImpIPersistStorage(void)
  40.     {
  41.     return;
  42.     }
  43.  
  44.  
  45.  
  46.  
  47. /*
  48.  * CImpIPersistStorage::QueryInterface
  49.  * CImpIPersistStorage::AddRef
  50.  * CImpIPersistStorage::Release
  51.  *
  52.  * Purpose:
  53.  *  Standard set of IUnknown members for this interface
  54.  */
  55.  
  56. STDMETHODIMP CImpIPersistStorage::QueryInterface(REFIID riid, LPVOID FAR *ppv)
  57.     {
  58.     return m_punkOuter->QueryInterface(riid, ppv);
  59.     }
  60.  
  61. STDMETHODIMP_(ULONG) CImpIPersistStorage::AddRef(void)
  62.     {
  63.     ++m_cRef;
  64.     return m_punkOuter->AddRef();
  65.     }
  66.  
  67. STDMETHODIMP_(ULONG) CImpIPersistStorage::Release(void)
  68.     {
  69.     --m_cRef;
  70.     return m_punkOuter->Release();
  71.     }
  72.  
  73.  
  74.  
  75.  
  76.  
  77. /*
  78.  * CImpIPersistStorage::GetClassID
  79.  *
  80.  * Purpose:
  81.  *  Returns the CLSID of the object represented by this interface.
  82.  */
  83.  
  84. STDMETHODIMP CImpIPersistStorage::GetClassID(LPCLSID pClsID)
  85.     {
  86.     *pClsID=m_pObj->m_clsID;
  87.     return NOERROR;
  88.     }
  89.  
  90.  
  91.  
  92.  
  93. /*
  94.  * CImpIPersistStorage::IsDirty
  95.  *
  96.  * Purpose:
  97.  *  Tells the caller if we have made changes to this object since
  98.  *  it was loaded or initialized new.
  99.  */
  100.  
  101. STDMETHODIMP CImpIPersistStorage::IsDirty(void)
  102.     {
  103.     /*
  104.      * Since we don't edit, we have no idea if this data is dirty.
  105.      * Delegate to the default handler in case it wants to ask the server.
  106.      */
  107.     return m_pObj->m_pDefIPersistStorage->IsDirty();
  108.     }
  109.  
  110.  
  111.  
  112.  
  113.  
  114. /*
  115.  * CImpIPersistStorage::InitNew
  116.  *
  117.  * Purpose:
  118.  *  Provides the object with the IStorage they can hold on to while
  119.  *  they are running.  Here we can initialize the structure of the
  120.  *  storage and AddRef it for incremental access.  This function will
  121.  *  only be called once in the object's lifetime in lieu of ::Load.
  122.  */
  123.  
  124. STDMETHODIMP CImpIPersistStorage::InitNew(LPSTORAGE pIStorage)
  125.     {
  126.     //Good time to initilize our data
  127.     m_pObj->m_pl.wVerMaj=VERSIONMAJOR;
  128.     m_pObj->m_pl.wVerMin=VERSIONMINOR;
  129.     m_pObj->m_pl.cPoints=0;
  130.     m_pObj->m_pl.rgbBackground=GetSysColor(COLOR_WINDOW);
  131.     m_pObj->m_pl.rgbLine=GetSysColor(COLOR_WINDOWTEXT);
  132.     m_pObj->m_pl.iLineStyle=PS_SOLID;
  133.  
  134.     //Make sure these aren't filled with trash.
  135.     _fmemcpy(&m_pObj->m_plContent,   &m_pObj->m_pl, CBPOLYLINEDATA);
  136.     _fmemcpy(&m_pObj->m_plThumbnail, &m_pObj->m_pl, CBPOLYLINEDATA);
  137.  
  138.  
  139.     m_pObj->m_pDefIPersistStorage->InitNew(pIStorage);
  140.     return NOERROR;
  141.     }
  142.  
  143.  
  144.  
  145.  
  146.  
  147. /*
  148.  * CImpIPersistStorage::Load
  149.  *
  150.  * Purpose:
  151.  *  Instructs the object to load itself from a previously saved IStorage
  152.  *  that was handled by ::Save in another object lifetime.  This function
  153.  *  will only be called once in the object's lifetime in lieu of ::InitNew.
  154.  */
  155.  
  156. STDMETHODIMP CImpIPersistStorage::Load(LPSTORAGE pIStorage)
  157.     {
  158.     POLYLINEDATA    pl;
  159.     ULONG           cb;
  160.     LPSTREAM        pIStream;
  161.     HRESULT         hr;
  162.  
  163.     if (NULL==pIStorage)
  164.         return ResultFromScode(STG_E_INVALIDPOINTER);
  165.  
  166.     //Open the CONTENTS stream
  167.     hr=pIStorage->OpenStream("CONTENTS", 0, STGM_DIRECT | STGM_READ
  168.         | STGM_SHARE_EXCLUSIVE, 0, &pIStream);
  169.  
  170.     if (FAILED(hr))
  171.         return ResultFromScode(STG_E_READFAULT);
  172.  
  173.     //Read all the data into the POLYLINEDATA structure.
  174.     hr=pIStream->Read((LPVOID)&pl, CBPOLYLINEDATA, &cb);
  175.     pIStream->Release();
  176.  
  177.     if (CBPOLYLINEDATA!=cb)
  178.         return ResultFromScode(STG_E_READFAULT);
  179.  
  180.     //Copy into the actual object now.
  181.     _fmemcpy(&m_pObj->m_pl, &pl, CBPOLYLINEDATA);
  182.  
  183.     m_pObj->m_pDefIPersistStorage->Load(pIStorage);
  184.     return NOERROR;
  185.     }
  186.  
  187.  
  188.  
  189.  
  190.  
  191. /*
  192.  * CImpIPersistStorage::Save
  193.  *
  194.  * Purpose:
  195.  *  Saves the data for this object to an IStorage.
  196.  */
  197.  
  198. STDMETHODIMP CImpIPersistStorage::Save(LPSTORAGE pIStorage, BOOL fSameAsLoad)
  199.     {
  200.     ULONG           cb;
  201.     LPSTREAM        pIStream;
  202.     HRESULT         hr;
  203.  
  204.     if (NULL==pIStorage)
  205.         return ResultFromScode(STG_E_INVALIDPOINTER);
  206.  
  207.     /*
  208.      * If the server is running, don't do the save ourselves since
  209.      * we'd end up writing the storage twice with possible conflicts.
  210.      */
  211.     if (OleIsRunning(m_pObj->m_pDefIOleObject))
  212.         return m_pObj->m_pDefIPersistStorage->Save(pIStorage, fSameAsLoad);
  213.  
  214.     //Rewrite the entire stream
  215.     WriteClassStg(pIStorage, m_pObj->m_clsID);
  216.     WriteFmtUserTypeStg(pIStorage, m_pObj->m_cf, "Polyline Figure");
  217.  
  218.     hr=pIStorage->CreateStream("CONTENTS", STGM_DIRECT | STGM_CREATE
  219.         | STGM_WRITE | STGM_SHARE_EXCLUSIVE, 0, 0, &pIStream);
  220.  
  221.     if (FAILED(hr))
  222.         return ResultFromScode(STG_E_WRITEFAULT);
  223.  
  224.     hr=pIStream->Write((LPVOID)&m_pObj->m_pl, CBPOLYLINEDATA, &cb);
  225.     pIStream->Release();
  226.  
  227.     m_pObj->m_pDefIPersistStorage->Save(pIStorage, fSameAsLoad);
  228.  
  229.     return (SUCCEEDED(hr) && CBPOLYLINEDATA==cb) ?
  230.         NOERROR : ResultFromScode(STG_E_WRITEFAULT);
  231.     }
  232.  
  233.  
  234.  
  235.  
  236. /*
  237.  * CImpIPersistStorage::SaveCompleted
  238.  * CImpIPersistStorage::HandsOffStorage
  239.  *
  240.  * Purpose:
  241.  *  Pass throughs.
  242.  */
  243.  
  244. STDMETHODIMP CImpIPersistStorage::SaveCompleted(LPSTORAGE pIStorage)
  245.     {
  246.     return m_pObj->m_pDefIPersistStorage->SaveCompleted(pIStorage);
  247.     }
  248.  
  249. STDMETHODIMP CImpIPersistStorage::HandsOffStorage(void)
  250.     {
  251.     return m_pObj->m_pDefIPersistStorage->HandsOffStorage();
  252.     }
  253.